﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBUtility;
namespace DeliveryManage.DAL
{
	/// <summary>
	/// 数据访问类:Pro_OrderGoods
	/// </summary>
	public partial class Pro_OrderGoods
	{
		public Pro_OrderGoods()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("Id", "Pro_OrderGoods"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Pro_OrderGoods");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(DeliveryManage.Model.Pro_OrderGoods model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Pro_OrderGoods(");
			strSql.Append("OrderId,GoodsId,GoodsName,IsActivity,BuyNum,CalcSpec,SpecNum,Price,GoodsAmount,AddPrice,Freight,AdvanceCharge,InvoicingCharge,TotalAmount)");
			strSql.Append(" values (");
			strSql.Append("@OrderId,@GoodsId,@GoodsName,@IsActivity,@BuyNum,@CalcSpec,@SpecNum,@Price,@GoodsAmount,@AddPrice,@Freight,@AdvanceCharge,@InvoicingCharge,@TotalAmount)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4),
					new SqlParameter("@GoodsId", SqlDbType.Int,4),
					new SqlParameter("@GoodsName", SqlDbType.NVarChar,30),
					new SqlParameter("@IsActivity", SqlDbType.Bit,1),
					new SqlParameter("@BuyNum", SqlDbType.Int,4),
					new SqlParameter("@CalcSpec", SqlDbType.Int,4),
					new SqlParameter("@SpecNum", SqlDbType.Int,4),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@GoodsAmount", SqlDbType.Decimal,9),
					new SqlParameter("@AddPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Freight", SqlDbType.Decimal,9),
					new SqlParameter("@AdvanceCharge", SqlDbType.Decimal,9),
					new SqlParameter("@InvoicingCharge", SqlDbType.Decimal,9),
					new SqlParameter("@TotalAmount", SqlDbType.Decimal,9)};
			parameters[0].Value = model.OrderId;
			parameters[1].Value = model.GoodsId;
			parameters[2].Value = model.GoodsName;
			parameters[3].Value = model.IsActivity;
			parameters[4].Value = model.BuyNum;
			parameters[5].Value = model.CalcSpec;
			parameters[6].Value = model.SpecNum;
			parameters[7].Value = model.Price;
			parameters[8].Value = model.GoodsAmount;
			parameters[9].Value = model.AddPrice;
			parameters[10].Value = model.Freight;
			parameters[11].Value = model.AdvanceCharge;
			parameters[12].Value = model.InvoicingCharge;
			parameters[13].Value = model.TotalAmount;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DeliveryManage.Model.Pro_OrderGoods model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Pro_OrderGoods set ");
			strSql.Append("OrderId=@OrderId,");
			strSql.Append("GoodsId=@GoodsId,");
			strSql.Append("GoodsName=@GoodsName,");
			strSql.Append("IsActivity=@IsActivity,");
			strSql.Append("BuyNum=@BuyNum,");
			strSql.Append("CalcSpec=@CalcSpec,");
			strSql.Append("SpecNum=@SpecNum,");
			strSql.Append("Price=@Price,");
			strSql.Append("GoodsAmount=@GoodsAmount,");
			strSql.Append("AddPrice=@AddPrice,");
			strSql.Append("Freight=@Freight,");
			strSql.Append("AdvanceCharge=@AdvanceCharge,");
			strSql.Append("InvoicingCharge=@InvoicingCharge,");
			strSql.Append("TotalAmount=@TotalAmount");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4),
					new SqlParameter("@GoodsId", SqlDbType.Int,4),
					new SqlParameter("@GoodsName", SqlDbType.NVarChar,30),
					new SqlParameter("@IsActivity", SqlDbType.Bit,1),
					new SqlParameter("@BuyNum", SqlDbType.Int,4),
					new SqlParameter("@CalcSpec", SqlDbType.Int,4),
					new SqlParameter("@SpecNum", SqlDbType.Int,4),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@GoodsAmount", SqlDbType.Decimal,9),
					new SqlParameter("@AddPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Freight", SqlDbType.Decimal,9),
					new SqlParameter("@AdvanceCharge", SqlDbType.Decimal,9),
					new SqlParameter("@InvoicingCharge", SqlDbType.Decimal,9),
					new SqlParameter("@TotalAmount", SqlDbType.Decimal,9),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.OrderId;
			parameters[1].Value = model.GoodsId;
			parameters[2].Value = model.GoodsName;
			parameters[3].Value = model.IsActivity;
			parameters[4].Value = model.BuyNum;
			parameters[5].Value = model.CalcSpec;
			parameters[6].Value = model.SpecNum;
			parameters[7].Value = model.Price;
			parameters[8].Value = model.GoodsAmount;
			parameters[9].Value = model.AddPrice;
			parameters[10].Value = model.Freight;
			parameters[11].Value = model.AdvanceCharge;
			parameters[12].Value = model.InvoicingCharge;
			parameters[13].Value = model.TotalAmount;
			parameters[14].Value = model.Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Pro_OrderGoods ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Pro_OrderGoods ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DeliveryManage.Model.Pro_OrderGoods GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Id,OrderId,GoodsId,GoodsName,IsActivity,BuyNum,CalcSpec,SpecNum,Price,GoodsAmount,AddPrice,Freight,AdvanceCharge,InvoicingCharge,TotalAmount from Pro_OrderGoods ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			DeliveryManage.Model.Pro_OrderGoods model=new DeliveryManage.Model.Pro_OrderGoods();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DeliveryManage.Model.Pro_OrderGoods DataRowToModel(DataRow row)
		{
			DeliveryManage.Model.Pro_OrderGoods model=new DeliveryManage.Model.Pro_OrderGoods();
			if (row != null)
			{
				if(row["Id"]!=null && row["Id"].ToString()!="")
				{
					model.Id=int.Parse(row["Id"].ToString());
				}
				if(row["OrderId"]!=null && row["OrderId"].ToString()!="")
				{
					model.OrderId=int.Parse(row["OrderId"].ToString());
				}
				if(row["GoodsId"]!=null && row["GoodsId"].ToString()!="")
				{
					model.GoodsId=int.Parse(row["GoodsId"].ToString());
				}
				if(row["GoodsName"]!=null)
				{
					model.GoodsName=row["GoodsName"].ToString();
				}
				if(row["IsActivity"]!=null && row["IsActivity"].ToString()!="")
				{
					if((row["IsActivity"].ToString()=="1")||(row["IsActivity"].ToString().ToLower()=="true"))
					{
						model.IsActivity=true;
					}
					else
					{
						model.IsActivity=false;
					}
				}
				if(row["BuyNum"]!=null && row["BuyNum"].ToString()!="")
				{
					model.BuyNum=int.Parse(row["BuyNum"].ToString());
				}
				if(row["CalcSpec"]!=null && row["CalcSpec"].ToString()!="")
				{
					model.CalcSpec=int.Parse(row["CalcSpec"].ToString());
				}
				if(row["SpecNum"]!=null && row["SpecNum"].ToString()!="")
				{
					model.SpecNum=int.Parse(row["SpecNum"].ToString());
				}
				if(row["Price"]!=null && row["Price"].ToString()!="")
				{
					model.Price=decimal.Parse(row["Price"].ToString());
				}
				if(row["GoodsAmount"]!=null && row["GoodsAmount"].ToString()!="")
				{
					model.GoodsAmount=decimal.Parse(row["GoodsAmount"].ToString());
				}
				if(row["AddPrice"]!=null && row["AddPrice"].ToString()!="")
				{
					model.AddPrice=decimal.Parse(row["AddPrice"].ToString());
				}
				if(row["Freight"]!=null && row["Freight"].ToString()!="")
				{
					model.Freight=decimal.Parse(row["Freight"].ToString());
				}
				if(row["AdvanceCharge"]!=null && row["AdvanceCharge"].ToString()!="")
				{
					model.AdvanceCharge=decimal.Parse(row["AdvanceCharge"].ToString());
				}
				if(row["InvoicingCharge"]!=null && row["InvoicingCharge"].ToString()!="")
				{
					model.InvoicingCharge=decimal.Parse(row["InvoicingCharge"].ToString());
				}
				if(row["TotalAmount"]!=null && row["TotalAmount"].ToString()!="")
				{
					model.TotalAmount=decimal.Parse(row["TotalAmount"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,OrderId,GoodsId,GoodsName,IsActivity,BuyNum,CalcSpec,SpecNum,Price,GoodsAmount,AddPrice,Freight,AdvanceCharge,InvoicingCharge,TotalAmount ");
			strSql.Append(" FROM Pro_OrderGoods ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Id,OrderId,GoodsId,GoodsName,IsActivity,BuyNum,CalcSpec,SpecNum,Price,GoodsAmount,AddPrice,Freight,AdvanceCharge,InvoicingCharge,TotalAmount ");
			strSql.Append(" FROM Pro_OrderGoods ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Pro_OrderGoods ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from Pro_OrderGoods T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Pro_OrderGoods";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

